package io.wdb.modules.generator.generator.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import io.wdb.common.utils.ConstantConfig;
import io.wdb.modules.generator.generator.dao.UserPointDao;
import io.wdb.modules.generator.generator.dao.UserPointLogDao;
import io.wdb.modules.generator.generator.entity.UserPointEntity;
import io.wdb.modules.generator.generator.entity.UserPointLogEntity;
import io.wdb.modules.generator.generator.service.UserPointService;



@Service("userPointService")
public class UserPointServiceImpl implements UserPointService {
	@Autowired
	private UserPointDao userPointDao;
	@Autowired
	private UserPointLogDao userPointLogDao;
	@Override
	public UserPointEntity queryObject(Long id){
		return userPointDao.queryObject(id);
	}
	
	@Override
	public List<UserPointEntity> queryList(Map<String, Object> map){
		return userPointDao.queryList(map);
	}
	
	@Override
	public int queryTotal(Map<String, Object> map){
		return userPointDao.queryTotal(map);
	}
	
	@Override
	public void save(UserPointEntity userPoint){
		userPointDao.save(userPoint);
	}
	
	@Override
	public void update(UserPointEntity userPoint){
		
		UserPointEntity userPointEntity = queryObject(userPoint.getId());
		
		if(!userPointEntity.getPointValue().equals(userPoint.getPointValue())) {
			//添加积分变更日志
			UserPointLogEntity userPointLogEntity = new UserPointLogEntity();
			userPointLogEntity.setChangeType(ConstantConfig.USER_POINT_LOG_CHANGETYPE_40);
			userPointLogEntity.setChangeValue(userPoint.getPointValue() - userPointEntity.getPointValue());//变更的积分
			userPointLogEntity.setCreateTime(new Date());
			userPointLogEntity.setPointId(userPoint.getId());
			
			userPointLogDao.save(userPointLogEntity);
		}
		
		userPointDao.update(userPoint);
	}
	
	@Override
	public void delete(Long id){
		userPointDao.delete(id);
	}
	
	@Override
	public void deleteBatch(Long[] ids){
		userPointDao.deleteBatch(ids);
	}
	
}
